package com.example.zce.library.data.sys.service;

import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.common.utils.UUIDUtils;
import com.example.zce.library.data.sys.dao.BackTaskMapper;
import com.example.zce.library.data.sys.entity.BackTask;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/8/5 20:36
 */
@Service
public class BackTaskService {

    @Autowired
    private BackTaskMapper backTaskMapper;

    public ResultTableBean<BackTask> findByParams(Map params) {
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()), Integer.valueOf(params.get("pageSize").toString()));
        List<BackTask> jobTasks = backTaskMapper.selectListByParams(params);
        PageInfo<BackTask> pageInfo = new PageInfo<>(jobTasks);
        ResultTableBean<BackTask> resultTableBean = new ResultTableBean<>(0,jobTasks,pageInfo.getTotal());
        return resultTableBean;
    }

    @Transactional
    public int saveOrUpdate(BackTask backTask) throws Exception{
        if (StringUtils.isNotEmpty(backTask.getId())){
            return backTaskMapper.updateByPrimaryKeySelective(backTask);
        }else{
            backTask.setId(UUIDUtils.getUUID32());
            backTask.setCreateTime(new Date());
            return backTaskMapper.insert(backTask);
        }
    }

    public BackTask findById(String id){
        return backTaskMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int delete(String id) throws Exception{
        return backTaskMapper.deleteByPrimaryKey(id);
    }
}
